import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/exam/dto/assessment.response.dart';
import 'package:ichinesereader/exam/widgets/BGExamImageWidget.dart';
import 'package:ichinesereader/exam/widgets/BGSinglePlayer.dart';
import 'package:ichinesereader/exam/widgets/SampleAudioPlayer.dart';
import 'package:ichinesereader/models/assessment/assessmentRecordDto.dart';
import '../../models/assessment/questionRecordSection.dart';

class ExamUtil {
  static AssessmentRecordDto buildAssessmentRecordDto(List<QuestionRecordSection> questionRecordSections, String? assessmentId, String? audioType,
      String? bookCode, String? kind, String? mode, String? questionGroupId, String? textType, String? type, String? userName) {
    var dto = AssessmentRecordDto();
    dto.assessmentId = assessmentId;
    dto.audioType = audioType;
    dto.bookCode = bookCode;
    dto.kind = kind;
    dto.mode = mode;
    dto.questionGroupId = questionGroupId;
    dto.textType = textType;
    dto.type = type;
    dto.userName = userName;

    if (questionRecordSections.isNotEmpty) {
      dto.record = buildRecordList(questionRecordSections);
    }
    return dto;
  }

  static AssessmentRecordDto buildRecordSettingDto(String? assessmentId, String? audioType, String? bookCode, String? kind, String? mode,
      String? questionGroupId, String? textType, String? type, String? userName) {
    var dto = AssessmentRecordDto();
    dto.assessmentId = assessmentId;
    dto.audioType = audioType;
    dto.bookCode = bookCode;
    dto.kind = kind;
    dto.mode = mode;
    dto.questionGroupId = questionGroupId;
    dto.textType = textType;
    dto.type = type;
    dto.userName = userName;
    return dto;
  }

  static List<Record> buildRecordList(List<QuestionRecordSection> questionRecordSections) {
    var records = <Record>[];
    questionRecordSections.forEach((section) {
      section.questions?.forEach((q) {
        var qid = q.id;
        q.question?.forEach((qq) {
          if (qq.answer != null) {
            records.add(Record(answer: qq.answer, number: qq.number, questionId: qid, intelligenceScore: qq.intelligenceScore));
          }
        });
      });
    });
    return records;
  }

 static Widget buildHeadInfoWidget(QuestionContent? element, BuildContext context) {
    if (element == null) return SizedBox();
    if (element.questionTitleNum == null) element.questionTitleNum = '';
    String text = element.questionTitleNum! + (element.text ?? '');
    List<Widget> options = [];
    if (element.image != null && element.image != '') {
      options.add(Padding(padding: EdgeInsets.only(bottom: 10), child: BGExamImageWidget(imgUrl: element.image, source: 'Content')));
    }
    if (element.audio == null) {
      options.add(HtmlWidget(text.replaceAll('\r\n', '<br/>'),
          textStyle: TextStyle(
            fontFamily: 'Noto Sans SC',
            fontSize: 16,
            color: (Get.isDarkMode ? Color(0xFF757575) : Colors.black87),
          )));
    } else {
      if (text.contains('<p>') || (element.json != null && element.json!.contains('%content%'))) {
        options.add(HtmlWidget(text.replaceAll('\r\n', '<br/>'),
            textStyle: TextStyle(
              fontFamily: 'Noto Sans SC',
              fontSize: 18,
              height: 1.5,
              wordSpacing: 1.5,
              color: (Get.isDarkMode ? Color(0xFF757575) : Colors.black87),
            )));
        if (element.json != null && element.json!.contains('%isTest%')) {
          options.add(Container(
              padding: EdgeInsets.fromLTRB(6, 4, 6, 0),
              height: 40,
              width: 52,
              child: IconButton(
                iconSize: 40,
                padding: EdgeInsets.zero,
                icon: Icon(
                  Icons.volume_up,
                  color: Colors.orange,
                ),
                onPressed: () {},
              )));
        } else {
          options.add(Padding(
              padding: EdgeInsets.only(top: 15),
              child: SampleAudioPlayer(
                name: 'audioPlayerTopicLevel',
                audioUrl: element.audio ?? '',
              )));
        }
      } else {
        options.add(RichText(
            text: TextSpan(
                text: text,
                style: TextStyle(
                  fontFamily: 'Noto Sans SC',
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                  color: (Get.isDarkMode ? Color(0xFF757575) : Colors.black87),
                ),
                children: [
              WidgetSpan(
                  child: Container(
                      padding: EdgeInsets.fromLTRB(6, 4, 6, 0),
                      height: 40,
                      width: 52,
                      child: IconButton(
                        iconSize: 40,
                        padding: EdgeInsets.zero,
                        icon: Icon(
                          Icons.volume_up,
                          color: Colors.orange,
                        ),
                        onPressed: () {
                          if (element.json == null || !element.json!.contains('%isTest%')) {
                            BGSinglePlayer().play(element.audio);
                          }
                        }
                      )))
            ])));
      }
    }

    return SizedBox(width: double.infinity, child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: options));
  }
}
